<template>
	<view class="pages-dynamic" :style="{background:pageColor}">
		<block v-if="isLoad">
			<fixed>
				<!-- #ifndef H5 -->
				<uni-nav-bar :fixed="false" :shadow="false" :statusBar="true" :title="navTitle" color="#ffffff"
					:backgroundColor="primaryColor" leftIcon="icon-left">
				</uni-nav-bar>
				<!-- #endif -->
				<view class="fix-info rel fill-base">
					<!-- #ifndef H5 -->
					<view class="space-top abs" :style="{background:primaryColor}"></view>
					<!-- #endif -->
					<view class="fill-base pt-lg pl-lg pr-lg pb-md search-info abs">
						<view class="flex-y-center">
							<image :src="detail.work_img" mode="aspectFill" class="d-cover"></image>
							<view class="pl-md">
								<view class="f-title text-bold">{{detail.coach_name || '-'}}</view>
								<view class="pt-sm flex-y-center" v-if="detail.id">
									<i class="iconfont" :class="[iconType[detail.text_type]]"
										:style="{color: primaryColor,fontSize: '14px'}"></i>
									<text class="f-caption" style="margin-left: 6rpx;"
										:style="{color:primaryColor}">{{configInfo.btn_config[detail.text_type*1-1].text}}</text>
								</view>
							</view>
						</view>
						<view class="d-info pl-lg pr-lg flex-between">
							<view class="">
								<view class="f-sm-title text-bold flex-center">{{detail.follow_num}}</view>
								<view class="f-caption c-paragraph flex-center">关注</view>
							</view>
							<view class="">
								<view class="f-sm-title text-bold flex-center">{{detail.dynamic_thumbs_num}}</view>
								<view class="f-caption c-paragraph flex-center">获赞</view>
							</view>
							<view class="f-caption follow-btn flex-center" @tap.stop="toCollect"
								:style="{color: !detail.is_follow ? '#fff' : primaryColor, background: !detail.is_follow? primaryColor : '#fff', borderColor: primaryColor}">
								{{detail.is_follow ? '已关注' : '关注' }}
							</view>
						</view>
					</view>
				</view>
			</fixed>

			<view class="space-md"></view>
			<wfalls-flow :list="list.data" :thumbsObj="thumbsObj" :path="1" :follow="true" ref="coach_dynamic_wfalls"
				v-if="list.data.length > 0" @collect="changeCollect"></wfalls-flow>
			<load-more :noMore="list.current_page>=list.last_page&&list.data.length>0" :loading="loading"
				v-if="loading">
			</load-more>

			<!-- #ifndef H5 -->
			<abnor v-if="!loading&&list.data.length<=0&&list.current_page==1"
				percent="calc(100vh - 184px - 88px - calc(30rpx + env(safe-area-inset-bottom) / 2))">
			</abnor>
			<!-- #endif -->
			<!-- #ifdef H5 -->
			<abnor v-if="!loading&&list.data.length<=0&&list.current_page==1"
				percent="calc(100vh - 184px - calc(30rpx + env(safe-area-inset-bottom) / 2))">
			</abnor>
			<!-- #endif -->

			<view class="space-footer"></view>
		</block>

	</view>
</template>

<script>
	import {
		mapState,
		mapActions,
		mapMutations
	} from "vuex"
	import siteInfo from '@/siteinfo.js';
	import wfallsFlow from "@/components/wfalls-flow.vue"
	export default {
		components: {
			wfallsFlow
		},
		data() {
			return {
				isLoad: false,
				options: {},
				navTitle: '动态',
				loading: true,
				lockTap: false,
				list: {
					data: []
				},
				param: {
					page: 1,
					coach_name: '',
					coach_id: 0
				},
				detail: {},
				iconType: {
					1: 'icon-kefuwu',
					2: 'iconjishifuwu',
					3: 'iconkeyuyue',
					4: 'iconkeyuyue'
				},
				thumbsObj: {}
			}
		},
		computed: mapState({
			pageActive: state => state.dynamic.pageActive,
			haveOperItem: state => state.dynamic.haveOperItem,
			activeIndex: state => state.dynamic.activeIndex,
			tabList: state => state.dynamic.tabList,
			coach_status: state => state.dynamic.coach_status,
			configInfo: state => state.config.configInfo,
			isGzhLogin: state => state.user.isGzhLogin,
			haveShieldOper: state => state.user.haveShieldOper
		}),
		async onLoad(options) {
			let {
				id = 0
			} = options
			this.options = options
			this.param.coach_id = id
			// #ifndef H5
			this.$util.showLoading()
			// #endif
			await this.initIndex()
			this.initNavTitle()
		},
		async onShow() {
			if (this.haveOperItem) {
				this.initRefresh()
				this.updateDynamicItem({
					key: 'haveOperItem',
					val: false
				})
			}
		},
		async onPullDownRefresh() {
			// #ifndef APP-PLUS
			uni.showNavigationBarLoading()
			// #endif
			await this.initRefresh();
			uni.stopPullDownRefresh()
		},
		async onReachBottom() {
			if (this.list.current_page >= this.list.last_page || this.loading) return;
			this.param.page = this.param.page + 1;
			this.loading = true;
			await this.getList();
			setTimeout(() => {
				this.$refs.coach_dynamic_wfalls.handleViewRender();
			}, 0)
		},
		watch: {
			locaRefuse(newval, oldval) {
				if (!newval) {
					this.toResetUtilLoca()
				}
			}
			// #ifndef H5
			,
			changeOnAddr(newval, oldval) {
				let {
					noloca,
				} = this.noChangeLoca
				if (newval && noloca) {
					this.initUtilLocaData()
				}
			},
			noChangeLoca(newval, oldval) {
				setTimeout(() => {
					let {
						lat,
						lng,
						unix = 0
					} = this.changeOnAddr
					let {
						noloca
					} = this.noChangeLoca
					let cur_unix = this.$util.DateToUnix(this.$util.formatTime(new Date(), 'YY-M-D h:m:s'))
					if (noloca && ((!lat && !lng) || !unix || (unix && (cur_unix - unix >= 1)))) {
						this.getUtilLocation()
					}
				}, 800)
			}
			// #endif
		},
		methods: {
			...mapActions(['getConfigInfo', 'getUserInfo']),
			...mapMutations(['updateUserItem', 'updateServiceItem', 'updateTechnicianItem', 'updateMapItem',
				'updateDynamicItem', 'updateShopstoreItem'
			]),
			async initIndex(refresh = false) {
				if (!this.configInfo.id || refresh) {
					await this.getConfigInfo()
				}
				await Promise.all([this.getDetail(), this.getList(1)])
				// #ifdef H5
				if (!refresh) {
					this.$jweixin.hideOptionMenu()
				}
				// #endif
			},
			async initRefresh() {
				await this.initIndex(true)
			},
			async initNavTitle() {
				this.$util.setNavigationBarColor({
					bg: this.primaryColor
				})
				let {
					tabBar
				} = this.configInfo
				if (tabBar && tabBar.length > 0) {
					let ind = tabBar.findIndex(item => {
						return item.id == 3
					})
					let navTitle = ind == -1 ? '动态' : tabBar[ind].name
					this.navTitle = navTitle
					// #ifdef H5
					uni.setNavigationBarTitle({
						title: navTitle
					})
					// #endif
				}
			},
			async getDetail() {
				let {
					id
				} = this.options
				let data = await this.$api.service.coachInfo({
					id
				})
				data.star = (data.star * 1).toFixed(1)
				this.detail = data
			},
			async getList(page) {
				if (page) {
					this.param.page = 1
					this.list.data = []
				}
				let {
					lat = 0, lng = 0
				} = this.location
				let {
					list: oldList,
				} = this
				let param = Object.assign({}, this.param, {
					lat,
					lng
				})

				let newList = await this.$api.dynamic.dynamicList(param)
				if (param.page == 1) {
					this.list = newList
				} else {
					newList.data = oldList.data.concat(newList.data)
					this.list = newList
				}
				this.list.data.forEach(item => {
					this.thumbsObj[item.id] = item.is_thumbs
				})
				this.isLoad = true
				this.loading = false
				this.$util.hideAll()
			},
			async changeCollect(id) {
				await this.$api.dynamic.thumbsAddOrCancek({
					dynamic_id: id
				})
				this.$util.showToast({
					title: '操作成功'
				})
				let thumbsObj = this.$util.deepCopy(this.thumbsObj)
				thumbsObj[id] = thumbsObj[id] == 1 ? thumbsObj[id] - 1 : thumbsObj[id] + 1
				this.thumbsObj = thumbsObj
				this.detail.dynamic_thumbs_num = thumbsObj[id] == 1 ? this.detail
					.dynamic_thumbs_num + 1 : this.detail.dynamic_thumbs_num - 1
			},
			async toCollect() {
				let {
					id,
					is_follow,
					follow_num
				} = this.detail
				await this.$api.dynamic.followAddOrCancek({
					coach_id: id
				})
				this.$util.showToast({
					title: is_follow ? '取消成功' : '关注成功'
				})
				this.detail.is_follow = is_follow == 1 ? 0 : 1
				this.detail.follow_num = is_follow == 1 ? follow_num - 1 :
					follow_num + 1
				this.$util.back()
			}
		}
	}
</script>


<style lang="scss">
	.pages-dynamic {
		.fix-info {
			height: 336rpx;

			.space-top {
				width: 100%;
				height: 40rpx;
				top: 0;
			}

			.search-info {
				width: 100%;
				top: 0;
				border-radius: 40rpx;
			}
		}

		.show-popup {
			width: 750rpx;
			background: #F3F3F5;
			border-radius: 30rpx 30rpx 0 0;

			.avatar {
				width: 84rpx;
				height: 84rpx;
			}

			.show-item {
				color: #222;
				height: 100rpx;
				background: #fff;
			}
		}

		.d-cover {
			width: 120rpx;
			height: 120rpx;
			border-radius: 120rpx;
		}

		.d-info {
			padding-top: 50rpx;
		}

		.follow-btn {
			width: 114rpx;
			height: 54rpx;
			border: 1px solid transparent;
			border-radius: 54rpx;
		}
	}
</style>